using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Adge.Common.Collections
{
    public struct ArrayToListAdapter<T> : IList<T>
    {
        private T[] array;

        public ArrayToListAdapter(T[] array)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            this.array = array;
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public int Count
        {
            get { return this.array.Length; }
        }

        public T this[int index]
        {
            get { return this.array[index]; }
            set { this.array[index] = value; }
        }

        public int IndexOf(T item)
        {
            return Array.IndexOf<T>(this.array, item);
        }

        void IList<T>.Insert(int index, T item)
        {
            throw new NotSupportedException();
        }

        void IList<T>.RemoveAt(int index)
        {
            throw new NotSupportedException();
        }

        void ICollection<T>.Add(T item)
        {
            throw new NotSupportedException();
        }

        void ICollection<T>.Clear()
        {
            throw new NotSupportedException();
        }

        public bool Contains(T item)
        {
            return IndexOf(item) >= 0;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            Array.Copy(this.array, 0, array, arrayIndex, this.array.Length);
        }

        bool ICollection<T>.Remove(T item)
        {
            throw new NotSupportedException();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return ((IEnumerable<T>)this.array).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.array.GetEnumerator();
        }
    }

    public static class Adapters
    {
        public static ArrayToListAdapter<T> ArrayToList<T>(T[] array)
        {
            return new ArrayToListAdapter<T>(array);
        }
    }

}
